//
//  MFAVplayer.m
//  27-Block播放按钮练习
//
//  Created by mafeng on 16/8/20.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "MFAVplayer.h"

@interface MFAVplayer ()
@property (nonatomic, strong) AVPlayerLayer *playLayer;

@end

@implementation MFAVplayer

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib {
    [super awakeFromNib];
    
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
      
        [self setPlayer];
    }
    return self;
}

- (void)setPlayer {
    NSURL *url = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
    self.playLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    
    // 设置显示图层是视频的宽高, 并使视频内容适应大小
    _playLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    // 显示图层要加到view的layer上面, 具体为什么我也不知道
    [self.layer addSublayer:_playLayer];
    [_player play];
    self.toolbarView.alpha = 0;
    NSLog(@"%lf", _player.currentTime);
}
- (void)layoutSubviews {
    [super layoutSubviews];
    // 设置layer的frame
    _playLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    // 设置进度条的内容
    [self.slider setThumbImage:[UIImage imageNamed:@"thumbImage"] forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:[UIImage imageNamed:@"MaximumTrackImage"] forState:UIControlStateNormal];
    [self.slider setMinimumTrackImage:[UIImage imageNamed:@"MinimumTrackImage"] forState:UIControlStateNormal];
}



@end
